import 'package:dmz/base/pageWidget/base_stateful_widget.dart';
import 'package:dmz/base/widget/defult_widget.dart';
import 'package:dmz/business/home/model/moto_model.dart';
import 'package:dmz/user/user_defurl.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class MineWidgets {
  static Widget mainWidget(
      List<MotoModel> motoList, int select, Function(int) click) {
    return Container(
        color: Colors.transparent,
        padding:
            EdgeInsets.only(top: Get.mediaQuery.padding.top + Get.height / 6),
        child: Container(
            padding: EdgeInsets.only(left: 32.sp, right: 32.sp),
            alignment: Alignment.center,
            decoration: BoxDecoration(
              gradient: const LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: [
                    Color(0xFF434650),
                    // Color(0xFF292A31),
                    // Color(0xFF2C2D35),
                    Color(0xFF2F3038),
                  ]),
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(80.sp),
                topRight: Radius.circular(80.sp),
              ),
              // color: const Color(0xFF434650)
            ),
            child: Column(children: [
              DefaultText(
                  EMIBUserManger().user!.phone!.isNotEmpty
                      ? ((EMIBUserManger().user?.phone ?? '')
                          .replaceRange(3, 7, '****'))
                      : '',
                  72.sp,
                  textColor: const Color(0xFFF9F9F9),
                  fontWeight: FontWeight.w700),
              DefaultText('你好，尊敬的车主'.tr, 28.sp,
                  textColor: const Color(0xFF999999)),
              Expanded(
                  child: ListView(
                      padding: const EdgeInsets.only(top: 0),
                      children: [
                    myGarageWidget(motoList, select, click),
                    mineListWidget(click)
                  ]))
            ]).marginOnly(top: 154.sp)));
  }

  static Widget headImageWidget() {
    return Container(
        alignment: Alignment.center,
        width: 300.sp,
        height: 300.sp,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(300.sp)),
            border: Border.all(color: const Color(0xFF23C565), width: 5.sp),
            color: const Color(0xFF434650)),
        child: DefaultNetWorkImage(
          EMIBUserManger().user?.picture ?? '',
          width: 298.sp,
          height: 298.sp,
          fit: BoxFit.cover,
        ).didClipRRectAll(298.sp));
  }

  static Widget myGarageWidget(
      List<MotoModel> motoList, int select, Function(int) click) {
    item(MotoModel data, bool selected) {
      return Container(
              width: 160.sp,
              height: 160.sp,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(48.sp)),
                  border: Border.all(
                      color: !selected
                          ? const Color(0xFF6F7178)
                          : const Color(0xFF23C565),
                      width: 5.sp),
                  color: const Color(0xFF434650)),
              child: data.sn_code == null
                  ? Image.asset('assets/images/user/add_icon.png',
                      width: 120.sp, height: 120.sp)
                  : DefaultNetWorkImage(
                      data.photo ?? '',
                      fit: BoxFit.contain,
                    ))
          .didClipRRectAll(48.sp)
          .marginOnly(right: 30.sp);
    }

    Widget rowItme(int indexed, int num) {
      return Row(
          children: List.generate(
              num,
              (index) => item(motoList[index + indexed * 4], select == index)
                  .addGestureDetector(() =>
                      index + indexed * 4 == motoList.length - 1
                          ? click(4)
                          : click(5)))).marginOnly(top: 56.sp);
    }

    int rowNum = motoList.length ~/ 4 + (motoList.length % 4 > 0 ? 1 : 0);
    return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          DefaultText('我的车库'.tr, 32.sp, textColor: const Color(0xFF999999)),
          Column(
              children: List.generate(
                  rowNum,
                  (index) => rowItme(index,
                      index < rowNum - 1 ? 4 : motoList.length - index * 4)))
        ]);
  }

  static mineListWidget(Function(int) click) {
    List<String> titles = ['消息通知'.tr, '失窃上报'.tr, '骑行记录'.tr, '在线报修'.tr];
    List<String> images = [
      'assets/images/user/news_icon.png',
      'assets/images/user/updata_icon.png',
      'assets/images/user/record_icon.png',
      'assets/images/user/repair_icon.png'
    ];
    item(title, image, Function() clicked, {String? content}) {
      return Container(
              padding: EdgeInsets.only(left: 40.sp, right: 40.sp),
              height: 160.sp,
              color: const Color(0xFF6F7178),
              child: Row(children: [
                Image.asset(image, width: 112.sp, height: 112.sp),
                Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      DefaultText(title, 40.sp,
                          textColor: const Color(0xFFF9F9F9),
                          fontWeight: FontWeight.w700),
                      if (content!.isNotEmpty)
                        DefaultText(content, 28.sp,
                            textColor: const Color(0xFFF9F9F9)),
                    ]),
                const Expanded(child: SizedBox()),
                Image.asset('assets/images/home/set/right_icon.png',
                    width: 18.sp, height: 28.sp)
              ]))
          .didClipRRectAll(160.sp)
          .marginOnly(bottom: 20.sp)
          .addGestureDetector(clicked);
    }

    return Column(
        children: List.generate(
            titles.length,
            (index) => item(titles[index], images[index], () => click(index),
                content: index == 1 ? '失窃上报服务仅对车主开放'.tr : ''))).marginOnly(
        top: 48.sp);
  }
}
